package com.alfred.mall.product.biz.service;

import com.alfred.common.framwork.util.ServiceExceptionUtil;
import com.alfred.mall.product.api.ProductAttrService;
import com.alfred.mall.product.api.bo.ProductAttrAndValuePairBO;
import com.alfred.mall.product.api.constant.ProductAttrConstants;
import com.alfred.mall.product.api.constant.ProductErrorCodeEnum;
import com.alfred.mall.product.biz.dao.ProductAttrMapper;
import com.alfred.mall.product.biz.dao.ProductAttrValueMapper;
import com.alfred.mall.product.biz.dataobject.ProductAttrDO;
import com.alfred.mall.product.biz.dataobject.ProductAttrValueDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@org.apache.dubbo.config.annotation.Service(validation = "true", version = "${dubbo.provider.ProductAttrService.version}")
public class ProductAttrServiceImpl implements ProductAttrService {

    @Autowired
    private ProductAttrValueMapper productAttrValueMapper;
    @Autowired
    private ProductAttrMapper productAttrMapper;

    public List<ProductAttrAndValuePairBO> validProductAttrAndValue(Set<Integer> productAttrValueIds, boolean validStatus) {
        //根据规格值ids查询规格值信息
        List<ProductAttrValueDO> attrValues = productAttrValueMapper.selectListByIds(productAttrValueIds);
        if (attrValues.size() != productAttrValueIds.size()) {
            //规格值数量和预期不匹配
            throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_NOT_EXIST.getCode());
        }
        if (validStatus) {
            for (ProductAttrValueDO attrValue : attrValues) {// 同时，校验下状态
                if (ProductAttrConstants.ATTR_STATUS_DISABLE.equals(attrValue.getStatus())) {
                    throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_NOT_EXIST.getCode());
                }
            }
        }
        //根据规格ids查询规格信息
        Set<Integer> attrIds = attrValues.stream().map(ProductAttrValueDO::getAttrId).collect(Collectors.toSet());
        List<ProductAttrDO> attrs = productAttrMapper.selectListByIds(attrIds);
        if (attrs.size() != attrIds.size()) {
            //规格数量和预期不匹配
            throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_ATTR_NOT_EXIST.getCode());
        }

        if (validStatus) {
            for (ProductAttrDO attr : attrs) {
                if (ProductAttrConstants.ATTR_VALUE_STATUS_DISABLE.equals(attr.getStatus())) {
                    throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_ATTR_NOT_EXIST.getCode());
                }
            }
        }
        Map<Integer, ProductAttrDO> attrMap = attrs.stream().collect(Collectors.toMap(ProductAttrDO::getId, ProductAttrDO -> ProductAttrDO));

        return attrValues.stream().map(productAttrValueDO -> new ProductAttrAndValuePairBO()
                .setAttrId(productAttrValueDO.getAttrId()).setAttrName(attrMap.get(productAttrValueDO.getAttrId()).getName())
                .setAttrValueId(productAttrValueDO.getId()).setAttrValueName(productAttrValueDO.getName())).collect(Collectors.toList());
    }

}
